package com.db.person.controller;

import com.db.person.bean.Job;
import com.db.person.bean.UserVisit;
import com.db.person.service.IJobService;
import com.db.person.util.PageModel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
public class JobController {

    @Autowired
    private IJobService jobService;

    @RequestMapping("/job/toedit")
    public String toEdit(Job job,Model model){

        model.addAttribute("job",job);

        return "job/edit";
    }

    @RequestMapping("/job/edit")
    public String edit(Job job){

        //把job里的数据 根据id进行update操作
        jobService.update(job);

        return "redirect:/job/list";
    }

    @RequestMapping("/job/toadd")
    public String toAdd(){
        return "job/add";
    }

    @RequestMapping("/job/add")
    public String add(Job job){

        //数据插入数据库
        jobService.save(job);

        return "redirect:/job/list";
    }

    @RequestMapping("/job/list")
    public String toList(Model model, PageModel pageModel){

        PageHelper.startPage(pageModel.getPageIndex(),pageModel.getPageSize());
        List<Job> all = jobService.getAll();
        PageInfo<Job> pageInfo = new PageInfo<>(all);

        long total = pageInfo.getTotal();//总条数
        pageModel.setRecordCount((int)total);

        List<Job> list = pageInfo.getList();//得到当前页的数据
        model.addAttribute("list",list);//展示的数据
        model.addAttribute("pageModel",pageModel);//前端需要分页的参数
        model.addAttribute("hre","job/list");//分页跳转的路径

        return "job/list";
    }

}
